// pages/sit/index.js
import WxValidate from "../../utils/WxValidate";
import {
  doLogin,
  register,
  orderRu,
  orderZhan,
  getUserStatus, del
} from '../../utils/login/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ["高中", "专科", "本科", "本科以上"],
    date: "2020-11-25",
    sex: '男',
    job: "否",
    edu: '高中',
    majorshow: false,
    oldjobshow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */

  //性别修改
  sexchange(e) {
    console.log(e);
    this.setData({
      sex: e.detail.value ? "女" : "男"
    })
  },

  //工作修改
  jobchange(e) {
    console.log(e);
    if (e.detail.value) {
      this.setData({
        oldjob: true
      })
    } else {
      this.setData({
        oldjob: false
      })
    }
    this.setData({
      job: e.detail.value ? "是" : "否",
      oldjobshow: e.detail.value ? true : false
    })
  },
  // 学历修改
  bindPickerChange(e) {
    console.log(e);
    this.setData({
      edu: this.data.array[e.detail.value],
      majorshow: e.detail.value == 0 ? false : true
    })
  },

  // 日期修改
  bindDateChange(e) {
    console.log(e);
    this.setData({
      date: e.detail.value
    })
  },


  //提交
  submit(e) {

    let params = e.detail.value;
    if (!this.WxValidate.checkForm(params)) {
      //表单元素验证不通过，此处给出相应提示
      let error = this.WxValidate.errorList[0];
      wx.showToast({
        title: error.msg,
        icon: 'none'
      })
      return false;
    } else {
    var obj = e.detail.value;
    obj.sex = obj.sex ? "女" : "男";
    obj.oldjob = obj.oldjob ? obj.oldjob : "否";
    obj.wxAppId='wxb2fbb734af59e1f5'
    obj.id = wx.getStorageSync('token')

    var newobj = {};
    for (var i in obj) {
      newobj[`xyUserLite.${i}`] = obj[i]
    }

    console.log(newobj);


    orderZhan(newobj).then(res => {
      console.log(res);
      if (res.statusCode == 200) {
        wx.showToast({
          title: "提交占座成功",
          duration: 1000
        })
        setTimeout(() => {
          wx.navigateBack({})
        }, 1000);
      }
    })
    }
  },

  // 表单验证规则
  initValidate() {
    let rules = {
      name: {
        required: true,
        maxlength: 10
      },
      age: {
        required: true,
        ranlgelength: [18, 30]
      },
      school: {
        required: true,
      },
      tel: {
        tel: true,
        required: true,
      },

    }

    let message = {
      name: {
        required: '请输入姓名',
        maxlength: '名字不能超过10个字'
      },
      age: {
        required: '请输入年龄',
        ranlgelength: '年龄需在18至30之间'
      },
      school: {
        required: '请输入毕业院校',
      },
      tel: {
        required: '请输入手机号',
      },

    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },


  onLoad: function () {
    this.initValidate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getUserStatus({
      id: wx.getStorageSync('token')
    }).then(res => {
      console.log(res.data.data);
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})